package jp.tkym.labs.ws;

import java.net.URL;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class MyWebSocektServer {
	private Server server;
	public static void main(String[] args) {
		try {
			new MyWebSocektServer().start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stop() throws Exception{
		if(server != null)
			server.stop();
		server = null;
	}
	
	public void start() throws Exception {
		MyWebSocketServlet webSocketServler = new MyWebSocketServlet();
		ServletHolder servletHolder = new ServletHolder(webSocketServler);
		ServletContextHandler servletContextHandler = new ServletContextHandler();
		servletContextHandler.addServlet(servletHolder, "/ws/*");

		HandlerList handlerList = new HandlerList();
		handlerList.addHandler(servletContextHandler);
		
		ResourceHandler resourceHandler = new ResourceHandler();
		URL resouceUrl = this.getClass().getResource("html");
		String external = resouceUrl.toExternalForm();
		resourceHandler.setResourceBase(external);
		handlerList.addHandler(resourceHandler);
		
		server = new Server(8040);
		server.setHandler(handlerList);

		server.start();
	}
}